package evocarcassonne.tileseteditor.ui.swing;

import evocarcassonne.core.board.Tile;
import evocarcassonne.core.utils.Card;
import evocarcassonne.tileseteditor.managers.TileManager;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class ConnectionsManager {

    static MyJCheckBox[] cbs;
    static Map<Tile, boolean[]> tileCombos;

    public static void init() {

        tileCombos = new HashMap();

        cbs = new MyJCheckBox[11];

        cbs[0] = new MyJCheckBox(0, "Nord - Est", 4, Card.N, Card.E);
        cbs[1] = new MyJCheckBox(1, "Nord - Sud", 5, Card.N, Card.S);
        cbs[2] = new MyJCheckBox(2, "Nord - Ouest", 3, Card.N, Card.W);
//  -----------------
        cbs[3] = new MyJCheckBox(3, "Sud - Est", 2, Card.S, Card.E);
        cbs[4] = new MyJCheckBox(4, "Sud - Ouest", 0, Card.S, Card.W);
//  -----------------
        cbs[5] = new MyJCheckBox(5, "Est - Ouest", 1, Card.W, Card.E);
//  -----------------
        cbs[6] = new MyJCheckBox(6, "Nord - Est - Ouest", -1, Card.N, Card.E, Card.W);
        cbs[7] = new MyJCheckBox(7, "Nord - Est - Sud", -1, Card.N, Card.E, Card.S);
        cbs[8] = new MyJCheckBox(8, "Nord - Ouest - Sud", -1, Card.N, Card.S, Card.W);
        cbs[9] = new MyJCheckBox(9, "Sud - Est - Ouest", -1, Card.S, Card.E, Card.W);
//  -----------------
        cbs[10] = new MyJCheckBox(10, "Nord - Sud - Est - Ouest", -1, Card.N, Card.E, Card.W, Card.S);

    }

    public static void addMapping() {
        tileCombos.put(TileManager.getTile(), null);
    }

    public static JCheckBox get(int i) {
        return cbs[i];
    }

    public static void reset() {

        if (cbs == null) {
            return;
        }

        for (MyJCheckBox cb : cbs) {
            cb.setEnabled(true);
            cb.setSelected(false);
        }
    }

    public static void setTileConnections() {

        reset();

        boolean[] values = tileCombos.get(TileManager.getTile());

        if (values != null) {

            for (int i = 0; i < cbs.length; i++) {

                cbs[i].setSelected(values[i]);

                // enable / disable others checkbox
                if (values[i]) {
                    for (int j = 0; j < cbs.length; j++) {
                        if ((i != j) && (cbs[i].comp != j)) {
                            cbs[j].setEnabled(false);
                        } else {
                            cbs[j].setEnabled(true);
                        }
                    }
                }
            }
        }
    }

    public static void resetConnections() {
        tileCombos.put(TileManager.getTile(), null);
    }

    private static class MyJCheckBox extends JCheckBox {

        public int comp;

        public MyJCheckBox(final int me, String text, final int comp, final int... cards) {

            super(text);

            this.comp = comp;

            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    JCheckBox src = (JCheckBox) e.getSource();
                    boolean value = src.isSelected();

                    boolean[] values = new boolean[cbs.length];

                    for (int i = 0; i < cbs.length; i++) {
                        if (i != comp && i != me) {
                            cbs[i].setEnabled(!value);
                            cbs[i].setSelected(false);
                        }

                        values[i] = cbs[i].isSelected();
                        tileCombos.put(TileManager.getTile(), values);
                    }

                    if (value) {
                        TileManager.addLink(cards);
                    } else {
                        reset();
                        TileManager.resetLinks();
                    }
                }
            });
        }
    }
}
